package JDBCUI;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Delete extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;

    JLabel jlpno = new JLabel("奖惩    编号：");

    JTextField jtpno = new JTextField("",20);

    JButton buttondelete = new JButton("删除");
    JButton buttonreturn = new JButton("返回");


    public Delete() {

        JPanel jpnumber = new JPanel();
        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jpnumber.add(jlpno);
        jpnumber.add(jtpno);

        jpforbutton.add(buttondelete);
        jpforbutton.add(buttonreturn);


        buttondelete.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String pno = jtpno.getText();

                Connection conn = null;
                ResultSet res = null;
                Statement stat = null;
                String sql = "DELETE FROM staff WHERE pno='"+pno+"'";
                String sql2="DELETE FROM huode WHERE getnum='"+pno+"'";
                try{
                    Class.forName("com.mysql.cj.jdbc.Driver");
                }catch(Exception a){
                    a.printStackTrace();
                }
                try{
                    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/work","root","123456");
                    stat = conn.createStatement();
                    int count =stat.executeUpdate(sql);
                               stat.executeUpdate(sql2);
                               if (count==0){
                                   if(e.getSource()==buttondelete){
                                       javax.swing.JOptionPane.showMessageDialog(null, "不存在！删除失败");
                                   }
                               }else {
                                   if(e.getSource()==buttondelete){
                                       javax.swing.JOptionPane.showMessageDialog(null, "删除成功");
                                   }
                               }

                }catch(SQLException h){

                    h.printStackTrace();
                }finally{
                    try{
                        conn.close();
                        System.out.println("删除操作结束");
                    }catch(SQLException j){
                        System.out.println("删除失败!！！！！！");
                        j.printStackTrace();
                    }
                }
            }
        });
        buttonreturn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                dispose();
            }
        });

        this.setTitle("删除教师奖惩信息");
        this.setLayout(new GridLayout(9,1));
        this.add(jpnumber);
        this.add(jpforbutton);
        this.setLocation(800,300);
        this.setSize(500,400);
        this.setVisible(true);
    }
}
